const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CompressionPlugin = require("compression-webpack-plugin");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const path = require('path');
const webpack = require('webpack')
module.exports = {
	outputDir: 'dist', //打包出来的文件夹名
	publicPath: '/ts/', // 署应用包时的基本 URL。 vue-router hash 模式使用
	productionSourceMap: false, //是否在构建生产包时生成打包后js文件夹里面的map文件
	lintOnSave: true, // 是否开启eslint保存检测
	assetsDir: 'static', //打包后放置文件
	indexPath: "index.html", //生成的 index.html 的输出路径
	pluginOptions: { //全局使用less
		'style-resources-loader': {
			preProcessor: 'less',
			patterns: [
				path.resolve(__dirname, './src/assets/css/mixin.less'),
			],
		},
	},
	// chainWebpack: config => {
	//     config.module.rule('images')
	//       .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
	//       .use('image-webpack-loader')
	//       .loader('image-webpack-loader')
	//       .options({ bypassOnDebug: true })
	//   },
	configureWebpack: {
		  plugins: [
		            new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
		        ],
		performance:{
       hints: 'warning',
       //入口起点的最大体积
       maxEntrypointSize: 500000,
       //生成文件的最大体积
       maxAssetSize: 300000,
       //只给出 js 文件的性能提示
       assetFilter: function (assetFilename) {
         return assetFilename.endsWith('.js');
       }
     },
		// 拆包，verdors.js
		optimization: {
				runtimeChunk: 'single',
				splitChunks: {
					chunks: 'all',
					maxInitialRequests: Infinity,
					minSize: 20000,
					cacheGroups: {
						vendor: {
							test: /[\\/]node_modules[\\/]/,
							name(module) {
								const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
								return `npm.${packageName.replace('@', '')}`
							}
						}
					}
				},
			minimizer: [
				new BundleAnalyzerPlugin(),
				new UglifyJsPlugin({//注释console
					uglifyOptions: {
						compress: {
							// warnings: false,
							drop_console: false, //注释console
							drop_debugger: false,
							pure_funcs: ['console.log'] //移除console
						}
					}
				}),
				// gzip压缩
				new CompressionPlugin({
					test: /\.(js|css|png|jpg)(\?.*)?$/i, //需要压缩的文件正则
					threshold: 10240, //文件大小大于这个值时启用压缩
					deleteOriginalAssets: false, //压缩后保留原文件
				})
			]
		},
		resolve: {
			// 组件图标按需引入
			alias: {
				'@ant-design/icons/lib/dist$': '@/plugins/icons.js'
			}
		},

	},

	css: {
		loaderOptions: {
			less: {
				javascriptEnabled: true
			}
		},
		extract: true, // 是否使用css分离插件 ExtractTextPlugin
		sourceMap: false, // 开启 CSS source maps
		requireModuleExtension: true // 启用 CSS modules for all css / pre-processor files.

	},
	devServer: { //生成环境接口代理地址
		port: 7878, // 端口
		open: true, // 启动后打开浏览器
		// proxy: {
		// proxyTable: {
		// '/api': {
		// 	target: 'http://localhost:8081', //设置你调用的接口域名和端口号
		// 	changeOrigin: true, //跨域
		// 	pathRewrite: {
		// 		'^/api': '/'
		// 	}
		// }
	},

}
